<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

Class ToolAction extends NewzanAction
{
	public function selectProducts()
	{
		$this->loadJS('custom.selectProductshelper','jquery.mCustomScrollbar.min','template');
		$from = date('Y-m-d', strtotime('-30 day'));
		$to = date('Y-m-d', strtotime('-1 day'));
		$this->assign('from', $from);
		$this->assign('to', $to);
		$this->assign('listItem', $this->_getSelectProducts($from, $to));
		// echo $this->_getSelectProducts();
		$this->display();
	}

	private function _getSelectProducts($from, $to)
	{
		$customSelect = M('shelpe_bos_products')->where(array('user_id'=> $this->me['user_id']))->getField('productId,link');
		
		if($customSelect) {
			$tmp = implode(',', $customSelect);
			$isMatched = preg_match_all('/id=([0-9]+)/', $tmp, $matches);
			unset($tmp);
			unset($customSelect);
			$customSelect = $matches[1];
			unset($matches);
		}

		$productMdl = new ProductModel;
		$listRes = $productMdl->getItemSeoData($this->me['user_id'], md5("{$from}_{$to}"), array('sort'=> 'pay_goods_num' , 'desc'=> 'desc'));
		$seoItem = array();
		if($listRes['total_items'] > 0) {
			// 	$listRes['pay_goods_num'] = array();
			// 	$listRes['avg_visitor_value'] = array();
			
			// foreach ($listRes['items'] as $key => $item) {
			// 	$listRes['items'][$key]['multiple'] = floatval($item['pay_goods_num'])*0.5 + floatval($item['avg_visitor_value'])*0.5;
			// 	$listRes['items'][$key]['is_custom'] = ($customSelect && in_array($item['goods_id'], $customSelect)) ? 1 : 0;
			// }
		 	foreach ($listRes['items'] as $key => $item) {
		 		$seoItem[$item['goods_id']] = $item;
		 		$seoItem[$item['goods_id']]['pay_goods_num_order'] = $key + 1;
		 		$seoItem[$item['goods_id']]['is_custom'] = ($customSelect && in_array($item['goods_id'], $customSelect)) ? 1 : 0;
		 	}

		 	$tmp = quickSort($listRes['items'], 'avg_visitor_value');
		 	foreach ($tmp as $key => $item) {
		 		if(isset($seoItem[$item['goods_id']])) {
		 			$seoItem[$item['goods_id']]['avg_visitor_value_order'] = $key + 1;
		 			$seoItem[$item['goods_id']]['multiple'] = ($key + 1)*0.5 + $seoItem[$item['goods_id']]['pay_goods_num_order']*0.5;
		 		}
		 	}

		 	$tmp = quickSort($listRes['items'], 'multiple');
			foreach ($tmp as $key => $item) {
		 		if(isset($seoItem[$item['goods_id']])) {
		 			$seoItem[$item['goods_id']]['multiple_order'] = $key + 1;
		 		}
		 	}
		 	$listRes['items'] = array_values($seoItem);
		 	unset($seoItem);
		 	unset($tmp);
		}

		$adgroupMdl = new AdgroupModel;
		$rptAdgroupMdl = new RptAdgroupModel;
		$adgroupInfo = $adgroupMdl->getUserAdgroupsAll($this->me['user_id'], true, array('online_status'=>'online'));
		$simbaAdgroup = array(
				'total_items' => count($adgroupInfo)
			);
		if($simbaAdgroup['total_items'] > 0) {
			$simbaAdgroup['items'] = array();
			$adgroup_iids = array_keys($adgroupInfo, true);
			$tmp = $rptAdgroupMdl->getAllAdgroupStat($this->me['user_id'], $adgroup_iids, 'roi,cost,sales',$from, $to, 'summary', array());
			foreach ($adgroupInfo as $key => $adgroup) {
				if($adgroup['title'] == '')
					continue;
				if(isset($simbaAdgroup['items'][$adgroup['num_iid']])) {
					$simbaAdgroup['items'][$adgroup['num_iid']]['cost'] += $tmp['summary'][$key]['cost'];
					$simbaAdgroup['items'][$adgroup['num_iid']]['sales'] += $tmp['summary'][$key]['sales'];
					$simbaAdgroup['items'][$adgroup['num_iid']]['roi'] = sprintf("%.2f", $simbaAdgroup['items'][$adgroup['num_iid']]['sales']/$simbaAdgroup['items'][$adgroup['num_iid']]['cost']);
				} else {
					$simbaAdgroup['items'][$adgroup['num_iid']] = array(
						'goods_id' => $adgroup['num_iid'],
						'goods_title' => $adgroup['title'],
						'goods_img' => $adgroup['img_url'],
						'roi' => $tmp['summary'][$key]['roi'],
						'cost' => $tmp['summary'][$key]['cost'],
						'sales' => $tmp['summary'][$key]['sales'],
						// 'simba_multiple' => $tmp['summary'][$key]['roi']*0.7 + $tmp['summary'][$key]['cost']*0.3,
						'is_custom' => ($customSelect && in_array($adgroup['num_iid'], $customSelect)) ? 1 : 0
					);
				}
			}
			$simbaAdgroup['item_iids'] = array_keys($simbaAdgroup['items']);
			$simbaAdgroup['items'] = array_values($simbaAdgroup['items']);
			$simbaAdgroup['total_items'] = count($simbaAdgroup['item_iids']);
			// unset($adgroupInfo);
			
			$simbaAdgroupTmp = array();

			$tmp = quickSort($simbaAdgroup['items'], 'cost');
			foreach ($tmp as $key => $item) {
				$simbaAdgroupTmp[$item['goods_id']] = $item;
	 			$simbaAdgroupTmp[$item['goods_id']]['cost_order'] = $key + 1;
		 	}

		 	$tmp = quickSort($simbaAdgroup['items'], 'roi');

		 	foreach ($tmp as $key => $item) {
	 			if(isset($simbaAdgroupTmp[$item['goods_id']])) {
	 				$simbaAdgroupTmp[$item['goods_id']]['roi_order'] = $key + 1;
	 				$simbaAdgroupTmp[$item['goods_id']]['simba_multiple'] = ($key + 1)*0.7 + $simbaAdgroupTmp[$item['goods_id']]['cost_order']*0.3;
	 			}
		 	}

		 	$tmp = quickSort($simbaAdgroup['items'], 'simba_multiple', 'asc');
			foreach ($tmp as $key => $item) {
		 		if(isset($simbaAdgroupTmp[$item['goods_id']])) {
		 			$simbaAdgroupTmp[$item['goods_id']]['simba_multiple_order'] = $key + 1;
		 		}
		 	}
		 	$simbaAdgroup['items'] = array_values($simbaAdgroupTmp);

		}

		return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode(array('seoItem'=> $listRes, 'simbaItem'=> $simbaAdgroup)));
	}
}